package com.mytest.leetcode.middle;

import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by shixi  on 2019/6/24
 */
@Slf4j
public class Solution1178 {

    public static void main(String[] args) {
        Solution1178 solution = new Solution1178();
        String s = "applepenapple";
        String [] wordDict = {"apple", "pen"};
        log.info("{}",solution.wordBreak(s, Arrays.asList(wordDict)));
    }

    public boolean wordBreak(String s, List<String> wordDict) {
        Set<String> wordDictSet = new HashSet(wordDict);
        boolean[] dp = new boolean[s.length() + 1];
        dp[0] = true;
        for (int i = 1; i <= s.length(); i++) {
            for (int j = 0; j < i; j++) {
//                log.info("j{},i{},{}", j, i, s.substring(j, i));
                if (dp[j] && wordDictSet.contains(s.substring(j, i))) {
//                    log.info("*************************j {},i {}", j, i);
                    dp[i] = true;
                    break;
                }
            }
        }
        return dp[s.length()];
    }
}

